//
//  MapViewController.m
//  easystudy
//
//  Created by 陈彦超 on 16/8/30.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "MapViewController.h"

@interface MapViewController ()
{
    CLLocation *myLocation;
    
    __weak IBOutlet UIButton *myLocationBtn;
    
}
@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //适配ios7
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0))
    {
        self.navigationController.navigationBar.translucent = NO;
    }
    
    _mapView.mapType = BMKMapTypeStandard;//地图类型
    [_mapView setZoomLevel:16];
    [_mapView setShowMapScaleBar:YES];
    _mapView.showsUserLocation = YES;
    _mapView.rotateEnabled = NO;        //设置地图不可以旋转
    _mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态
    
    //定位相关
    locationService = [[BMKLocationService alloc] init];
}

-(void)viewWillAppear:(BOOL)animated
{
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    //定位相关
    locationService.delegate = self;
    [locationService startUserLocationService];
    
    if (_isForTargetLocation) {
        myLocationBtn.hidden = YES;
        BMKPointAnnotation *point = [[BMKPointAnnotation alloc] init];
        [point setCoordinate:CLLocationCoordinate2DMake([_targetLatitude floatValue], [_targetLongitude floatValue])];
        point.title = _targetAddress;
        [_mapView addAnnotation:point];
        [_mapView selectAnnotation:point animated:YES];
        [_mapView setCenterCoordinate:CLLocationCoordinate2DMake([_targetLatitude floatValue], [_targetLongitude floatValue]) animated:YES];
        
    }
    
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
    [locationService stopUserLocationService];
    locationService.delegate = nil;
}


#pragma mark BMKLocationServiceDelegate
/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    [locationService stopUserLocationService];
    [_mapView updateLocationData:userLocation];//用户位置
    
    myLocation = userLocation.location;
    if (!_isForTargetLocation) {
        _mapView.centerCoordinate = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude);
    }
}

/**
 *定位失败后，会调用此函数
 *@param mapView 地图View
 *@param error 错误号，参考CLError.h中定义的错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"定位失败了");
}




- (IBAction)backBtn:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)toMyLocationBtn:(UIButton *)sender
{
    CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude);
    [_mapView setCenterCoordinate:coor animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
